<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9/009
 * Time: 10:41
 */

namespace app\api\model;

use app\api\model\Region as RegionModel;
use app\api\model\FranchiseArea as FranchiseAreaModel;
use app\api\model\FranchiseImage as FranchiseImageModel;
use app\api\service\Token;
use app\lib\exception\AddressException;

class Franchise extends BaseModel {

    /**
     * 特许商城图片
     * @return \think\model\relation\hasMany
     */
    public function image() {
        return $this->hasMany('FranchiseImage');
    }

    /**
     * 特许商城订单
     * @return \think\model\relation\hasMany
     */
    public function orders() {
        return $this->hasMany('FranchiseOrder');
    }

    /**
     * 特许商城详情
     * @return \think\model\relation\hasOne
     */
    public function content() {
        return $this->hasOne('FranchiseContent', '', '', 'LEFT');
    }

    /**
     * 获取特许商城列表
     * @param $address
     * @return mixed
     */
    public static function franchiseList($address) {

        $address = self::get_address($address);

        $franchise = FranchiseAreaModel::FranchiseList($address);

        $list = [];
        foreach ($franchise as $k => $v) {
//            dump($v);
            $cover = FranchiseImageModel::get(['franchise_id' => $v->id, 'type' => 'cover', 'delete' => 0]);
            $list[] = [
                'cover' => get_img_url($cover['image']),
                'name' => $v['name'],
                'industry' => $v->industry,
                'id' => $v->id,
            ];
        }
        $data['list'] = $list;
        if (!input('?param.page') || input('param.page') == 1) {
            $count = FranchiseAreaModel::FranchiseCount($address);
            $data['count'] = $count;
        }

        return $data;
    }

    /**
     * 处理前端传过来的地址数据
     * @param $address
     * @return array
     * @throws AddressException
     */
    public static function get_address($address) {
        $address = explode(',', $address);
//        dump($address);
        if (count($address) == 3) {
            if (is_numeric($address[0])) {
                return $address;
            } else {
                $province = RegionModel::get(['region_name' => $address[0]]);
                if ($province) {
                    if ($address[1]) {
                        $city = RegionModel::get(['region_name' => $address[1], 'parent_id' => $province['region_id']]);
                    } else {
                        $city = RegionModel::get(['parent_id' => $province['region_id']]);
                    }
                    if ($city) {
                        $area = RegionModel::get(['region_name' => $address[2], 'parent_id' => $city['region_id']]);
                    }
                }
            }
        }

        if (isset($province) && $province['region_id'] > 0
            && isset($city) && $city['region_id'] > 0
            && isset($area) && $area['region_id'] > 0) {
            return [$province['region_id'], $city['region_id'], $area['region_id']];
        } else {
            throw new AddressException([
                'msg' => '特许商城地址参数异常',
                'errorCode' => 12003
            ]);
        }
    }

    /**
     * 获取特许商城详情
     * @param $id
     * @return null|\think\Model
     */
    public static function franchiseListInfo($id) {

        $uid = Token::getCurrentUid();

        $franchise = self::get(['status' => 1, 'delete_time' => 0, 'id' => $id], ['content']);
        $franchise['cover'] = get_img_url($franchise->image()->where('type', 'cover')->where('delete', 0)->value('image'));
        $franchise['logo'] = get_img_url($franchise->image()->where('type', 'logo')->where('delete', 0)->value('image'));
        $franchise->content->content = str_replace("/ueditor/php/upload/",config('setting.img_domain') . "/ueditor/php/upload/", $franchise->content->content);
        // 是否已支付
        $franchise['is_pay'] = $franchise->orders()->where('member_id', $uid)->where('status', 2)->count();

        return $franchise;
    }
}